/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.pages.member')
        .controller('ParentEntrustCtrl', ParentEntrustCtrl);

    function ParentEntrustCtrl($scope,$state,EntrustModel,AppUtils,store,LoginAccountAuth) {

        $scope.$on('$ionicView.beforeEnter', function (event, viewData) {
            viewData.enableBack = true;
        });

        $scope.gotoDetail = function(entrustId){
            if(!AppUtils.isUndefinedOrNull(entrustId)){
                $state.go("entrust.entrustInfo",{entrustId:entrustId});
            }else{
                AppUtils.showAlert("请选择要查看的嘱托！");
            }
        }

        var vm = this;
        vm.loginAccount = {};
        vm.exhortaions = [];
        vm.loadMoreBtnStatus = true;
        vm.loadMoreBtnStr = "加载更多";
        vm.pageIndex = 1;//第一页

        $scope.loadMoreEntrust = function(){
            if(vm.loadMoreBtnStatus){
                vm.pageIndex = vm.pageIndex + 1;
                vm.getEntrustPage();
            }
        }

        var loginAccount = LoginAccountAuth.getAccount()
            .then(function(data){
                vm.loginAccount = data;
                vm.getEntrustPage();
            });
        vm.getEntrustPage = function(){
            EntrustModel.getEntrustPage({
                entrustId: vm.loginAccount.memberId,
                pageInfo: {
                    pageSize: 5,
                    pageNumber: vm.pageIndex
                }
            }).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.showAlert(response.msg);
                }else{
                    var reponseDate = response.pageResult;
                    var items = reponseDate.pageResult;
                    var pageIndex = reponseDate.pageIndex;
                    var pageTotalPageSize = reponseDate.pageTotalPageSize;
                    if(pageIndex == pageTotalPageSize || items.length == 0){
                        vm.loadMoreBtnStatus = false;
                        vm.loadMoreBtnStr = "没有更多数据了";
                    }
                    angular.forEach(items,function(data){
                        vm.exhortaions.push(data);
                    })
                }
            })
        }

        vm.entrustTitle = "嘱托列表";
        vm.isUnsolved = true;

        vm.clientType = AppUtils.getClientType();
        if(vm.clientType === "ios"){
            vm.showMenuBoxType = "ios";
            var $iosActionsheet = $('#iosActionsheet');
            var $iosMask = $('#iosMask');
            var hideActionSheet = function() {
                $iosActionsheet.removeClass('weui-actionsheet_toggle');
                $iosMask.fadeOut(200);
            }
            $iosMask.on('click', hideActionSheet);
            $('#iosActionsheetCancel').on('click', hideActionSheet);
            $("#showIOSActionSheet").on("click", function(){
                $iosActionsheet.addClass('weui-actionsheet_toggle');
                $iosMask.fadeIn(200);
            });
        }else{
            vm.showMenuBoxType = "android";
            var $androidActionSheet = $('#androidActionsheet');
            var $androidMask = $androidActionSheet.find('.weui-mask');
            $("#showAndroidActionSheet").on('click', function(){
                $androidActionSheet.fadeIn(200);
                $androidMask.on('click',function () {
                    $androidActionSheet.fadeOut(200);
                });
            });
        }

        $scope.$on('$ionicView.beforeLeave', function(){
            $(".weui-mask").click();
            $("#iosActionsheetCancel").click();
        });

    }

})();